<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="审核结果" prop="check_status">
        <el-select v-model="queryParams.check_status" placeholder="请选择审核结果" clearable>
          <el-option
            v-for="dict in dict.type.flow_check_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['flow:step:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stepList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="业务编号" align="center" prop="task_num" />
      <el-table-column label="业务主题" align="center" prop="task_title" />
      <el-table-column label="申请时间" align="center" prop="check_time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.check_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人姓名" align="center" prop="check_user_name" />
      <el-table-column label="审核结果" align="center" prop="check_status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flow_check_status" :value="scope.row.check_status"/>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="check_time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.check_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核不通过原因" align="center" prop="check_reason" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pager.page"
      :limit.sync="pager.limit"
      @pagination="getList"
    />
    <!-- 添加或修改审批步骤对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="业务主键" prop="task_id">
          <el-input v-model="form.task_id" placeholder="请输入业务主键" />
        </el-form-item>
        <el-form-item label="审核人" prop="check_user_id">
          <el-input v-model="form.check_user_id" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="审核人姓名" prop="check_user_name">
          <el-input v-model="form.check_user_name" placeholder="请输入审核人姓名" />
        </el-form-item>
        <el-form-item label="审核结果" prop="check_status">
          <el-radio-group v-model="form.check_status">
            <el-radio
              v-for="dict in dict.type.flow_check_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核时间" prop="check_time">
          <el-date-picker clearable
            v-model="form.check_time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核不通过原因" prop="check_reason">
          <el-input v-model="form.check_reason" placeholder="请输入审核不通过原因" />
        </el-form-item>
        <el-form-item label="上一步骤" prop="pid">
          <el-input v-model="form.pid" placeholder="请输入上一步骤" />
        </el-form-item>
        <el-form-item label="忽略标识" prop="ignore_flag">
          <el-input v-model="form.ignore_flag" placeholder="请输入忽略标识" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <import-dialog :open="upload" model="flow_step" @cancel="importCancel" @success="importSuccess" />
  </div>
</template>

<script>
import { apiGet, apiPost, apiPut, apiDelete, apiDs, apiExport, apiDownload } from "@/api/openapi";
import ImportDialog from "@/components/ImportDialog";

export default {
  name: "Step",
  components: {
    ImportDialog
  },
  dicts: ['flow_check_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审批步骤表格数据
      stepList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        check_status: null,
        ignore_flag: "0",
        "@alias": "t",
        "@exp": {
          // 忽略标识模糊搜索
          check_user_name: 'lk',
        }
      },
      pager: {
        page: 1,
        limit: 10,
        order: "id desc",
      },
      // 导入参数
      upload: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        task_id: [
          { required: true, message: "业务主键不能为空", trigger: "blur" }
        ],
        check_user_id: [
          { required: true, message: "审核人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询审批步骤列表 */
    getList() {
      this.loading = true;
      let params = {};
      params['flow_step'] = this.queryParams;
      params['flow_task'] = {
        "@column": "title:task_title, num:task_num, apply_time",
        "@alias": "s",
        "@join": "s.id = t.task_id"
      }
      params['@pager'] = this.pager;
      apiGet(params).then(response => {
        this.stepList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        task_id: null,
        check_user_id: null,
        check_user_name: null,
        check_status: null,
        check_time: null,
        check_reason: null,
        pid: null,
        ignore_flag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pager.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加审批步骤";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
        let data = {};
        data['flow_step'] = { id:id};
      apiGet(data).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改审批步骤";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            let data = {};
            data['flow_step'] = this.form;
            data['@tags'] = ['flow_step'];
          if (this.form.id != null) {
            apiPut(data).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            apiPost(data).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的审批步骤数据？').then(function() {
        let data = {};
        data['flow_step'] = { id:ids, "@exp":{id:"in"}};
        data['@tags'] = ['flow_step'];
        return apiDelete(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload = true;
    },
    importSuccess(msg) {
        this.upload = false;
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
    },
    importCancel() {
        this.upload = false;
    },
    /** 导出按钮操作 */
    handleExport() {
        let params = {};
      params['flow_step'] = this.queryParams;
      params['@pager'] = {page:1, limit:10000};
      apiExport(params).then(response => {
        apiDownload(response.msg);
      });
    }
  }
};
</script>
